package com.csi.dao.impl;

import com.csi.dao.EmployDao;
import com.csi.domain.Employ;
import com.csi.utils.DateUtils;
import com.csi.utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmployDaoImpl extends JDBCUtils implements EmployDao {
    DateUtils dateUtils =new DateUtils();

    @Override
    public Employ select(int employNumber, String employPassword) throws SQLException {
        final String sql="select * from hotel_employ where employNumber=? and employPassword=?";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,employNumber);
        ps.setString(2,employPassword);
        ResultSet rs = ps.executeQuery();
        Employ employ=null;
        if(rs.next()){
            employ=new Employ();
            employ.setEmployId(rs.getInt("employId"));
            employ.setEmployNumber(employNumber);
            employ.setWages(rs.getInt("wages"));
            employ.setUpDates(rs.getTime("upDates"));
            employ.setDownDate(rs.getTime("downDate"));
            employ.setEmployUsername(rs.getString("employUsername"));
            employ.setEmployGrade(rs.getString("employGrade"));

        }
        release(ps,connection);
        return employ;
    }

    @Override
    public int add(Employ employ) throws SQLException {
        final String sql="insert into hotel_employ(employNumber,employPassword,wages,upDates,downDate,employUsername,employGrade)values(?,?,?,?,?,?,?)";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,employ.getEmployNumber());
        ps.setInt(2,123456);
        ps.setInt(3,employ.getWages());
        ps.setTime(4,employ.getUpDates());
        ps.setTime(5,employ.getDownDate());
        ps.setString(6,employ.getEmployUsername());
//        System.out.println(employ.getEmployGrade());
        ps.setString(7,employ.getEmployGrade());
        int i = ps.executeUpdate();
        release(ps,connection);
        return i;
    }

    @Override
    public int delete(int employId) throws SQLException {
        final String sql="delete from hotel_employ where employid=?";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,employId);
        int i = ps.executeUpdate();
        release(ps,connection);
        return i;
    }

    @Override
    public int update(Employ employ) throws SQLException {
        final String sql="update hotel_employ set employNumber=?,wages=?,updates=?,downdate=?,employusername=?,employgrade=? where employid=?";
        Connection connection=this.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,employ.getEmployNumber());
        ps.setInt(2,employ.getWages());
        ps.setTime(3,employ.getUpDates());
        ps.setTime(4,employ.getDownDate());
        ps.setString(5,employ.getEmployUsername());
        ps.setString(6,employ.getEmployGrade());
        ps.setInt(7,employ.getEmployId());
        int i=ps.executeUpdate();
        release(ps,connection);
        return i;
    }

    @Override
    public List<Employ> list() throws SQLException {
        final String SQL="SELECT * FROM `hotel_employ`";
        Connection connection = this.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQL);
        ResultSet rs = ps.executeQuery();
        List<Employ> employs =new ArrayList<>();
        while (rs.next()){
            Employ employ =new Employ();
            employ.setEmployId(rs.getInt("EmployId"));
            employ.setEmployNumber(rs.getInt("EmployNumber"));
//            employ.setEmployPassword(rs.getString("EmployPassword"));
            employ.setWages(rs.getInt("Wages"));
            employ.setUpDates(rs.getTime("UpDates"));
            employ.setDownDate(rs.getTime("DownDate"));
            employ.setEmployUsername(rs.getString("EmployUsername"));
            employ.setEmployGrade(rs.getString("EmployGrade"));
//            if(rs.getInt("EmployGrade")==1){
                employs.add(employ);
//            }

        }
        release(rs,ps,connection);
        return employs;
    }
}
